package com.marcocirillo.justweet;

import javax.swing.*;
import twitter4j.TwitterException;
import java.awt.*;

public class View extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5804229778148923455L;
	/*private JLabel usernameLabel;
	private JLabel passwordLabel;
	private JTextField usernameText;
	private JPasswordField passwordText;
	*/
	private JTextArea tweetText;
	private JButton tweetButton;
	private JLabel currentStatusLabel;
	private JTextArea currentStatus;
	//private JPanel userInfoPanel;
	private JPanel tweetPanel;
	private JPanel statusPanel;
	private JusTweet jt;
	
	public static final String USERNAME_TEXT = "Username: ";
	public static final String PASSWORD_TEXT = "Password: ";
	public static final String TWEET_TEXT = "Tweet!";
	public static final String CURRENT_STATUS_TEXT = "Current Status: ";
	
	public View(int width, int height, JusTweet jt)
	{
		/* Construct Window */
		super("JusTweet");
		System.out.println("window created");
		super.setSize(width, height);
		super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		super.setLayout(new GridLayout(0,1)/* new FlowLayout()*/);
		
	    try {
	        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
	    } catch (Exception evt) {}
		
	    /*if (jt == null)
		{
	    	System.out.println("JT is null");
	    	this.openLoginWindow(jt);
		} else
		{
			this.jt = jt;
		}*/
	    this.jt = jt;
	    System.out.println("Constructing Panels");
	    /* Construct Panels */
	    //userInfoPanel = new JPanel(new FlowLayout());
	    tweetPanel = new JPanel(new FlowLayout());
	    statusPanel = new JPanel(new FlowLayout());
		
		/* Construct Widgets */
		/*usernameLabel = new JLabel(View.USERNAME_TEXT);
		passwordLabel = new JLabel(View.PASSWORD_TEXT);
		usernameText = new JTextField("", 15);
		passwordText = new JPasswordField("", 15);
		passwordText.setEchoChar('*');
		*/
		tweetText = new JTextArea(5, 50);
		tweetText.setAutoscrolls(true);
		tweetButton = new JButton(View.TWEET_TEXT);
		/*JScrollPane areaScrollPane = new JScrollPane(tweetText);
		areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		//areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		areaScrollPane.setPreferredSize(new Dimension(250, 250));*/
		tweetText.setLineWrap(true);
		currentStatusLabel = new JLabel(View.CURRENT_STATUS_TEXT);
		currentStatus = new JTextArea(5,50);

		/* Add ActionListeners */
		tweetButton.addActionListener(new Controller(this.jt, this));
		tweetButton.setActionCommand(View.TWEET_TEXT);
		/* Pack Window! */
		/*userInfoPanel.add(usernameLabel);
		userInfoPanel.add(usernameText);
		userInfoPanel.add(passwordLabel);
		userInfoPanel.add(passwordText);*/
		tweetPanel.add(tweetText);
		tweetPanel.add(tweetButton);
		statusPanel.add(currentStatusLabel);
		statusPanel.add(currentStatus);
		//this.add(userInfoPanel);
		this.add(tweetPanel);
		this.add(statusPanel);
		this.pack();
		
	}
	
	public void setCurrentStatus(JusTweet jt) throws TwitterException
	{
		String status = jt.getCurrentStatus().getText();
		this.currentStatus.setText(status);
	}
	
	public void clearTweetText()
	{
		this.tweetText.setText("");
	}
	
	public String getTweetText()
	{
		return tweetText.getText();
	}
	/*
	public String getUsernameText()
	{
		return usernameText.getText();
	}
	public String getPasswordText()
	{
		char[] c = passwordText.getPassword();
		String s = new String(c);
		return s;
	}
	*/
	public void openLoginWindow(JusTweet jt)
	{
		//JusTweet newLogin = null;
		System.out.println("Login Window Opened");
		LoginWindow lw = new LoginWindow(500, 300, jt);
		lw.setVisible(true);
		//newLogin = lw.getJusTweet();
		//return newLogin;
	}
	
	public void setJusTweet(JusTweet jt)
	{
		this.jt = jt;
	}

}
